//
//  MGTimer.m
//  MGPublic
//
//  Created by lhongl on 2025/10/11.
//

#import "MGTimer.h"
@interface MGTimer ()

@property (nonatomic, strong) dispatch_source_t timer;

@property (nonatomic, strong) dispatch_semaphore_t lock;

@property (nonatomic, assign) BOOL onFire;

@property (nonatomic, assign) MGTimerStatus timerStatus;

@end
@implementation MGTimer

- (instancetype)init{
    self = [super init];
    if (self) {
        _timerStatus = MGTimerStatusStop;
    }
    return self;
}

- (void)startTimerWithSpace:(float)timeNum block:(void(^)(BOOL))block {
    if (_timerStatus != MGTimerStatusStop) {
        return;
    }
    dispatch_time_t start = dispatch_walltime(NULL, (int64_t)(0.0 * NSEC_PER_SEC));
    uint64_t interval = (uint64_t)(timeNum * NSEC_PER_SEC);
    dispatch_source_set_timer(self.timer, start, interval, 0);
    dispatch_source_set_event_handler(self.timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (block) {
                block(YES);
            }
        });
    });
    [self resumeTime];
}

- (void)resume {
    if (_timerStatus != MGTimerStatusSuspend) {
        return;
    }
    [self resumeTime];
}

- (void)resumeTime {
    dispatch_semaphore_wait(self.lock, DISPATCH_TIME_FOREVER);
    if (self.timer) {
        dispatch_resume(self.timer);
        _onFire = YES;
        _timerStatus = MGTimerStatusIng;
    }
    dispatch_semaphore_signal(self.lock);
}

/// 暂停
- (void)suspend {
    if (_timerStatus != MGTimerStatusIng) {
        return;
    }
    dispatch_semaphore_wait(self.lock, DISPATCH_TIME_FOREVER);
    if (self.timer) {
        dispatch_suspend(self.timer);
        _onFire = NO;
        _timerStatus = MGTimerStatusSuspend;
    }
    dispatch_semaphore_signal(self.lock);
}

/// 停止
- (void)stopTimer {
    if (_timerStatus != MGTimerStatusIng) {
        return;
    }
    dispatch_semaphore_wait(self.lock, DISPATCH_TIME_FOREVER);
    if (self.timer) {
        dispatch_source_cancel(self.timer);
        _timerStatus = MGTimerStatusStop;
        _onFire = NO;
        self.timer = NULL;
    }
    dispatch_semaphore_signal(self.lock);
}


/// 消失
- (void)dealloc {
    if (self.timer) {
        if (_onFire == NO) {
            dispatch_resume(self.timer);
        }
    }
    [self stopTimer];
}


#pragma mark - getter
- (dispatch_source_t)timer {
    if (_timer == nil) {
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
        _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    }
    return _timer;
}

- (dispatch_semaphore_t)lock {
    if (_lock == nil) {
        _lock = dispatch_semaphore_create(1);
    }
    return _lock;
}

- (MGTimerStatus)timerStatu{
    return self.timerStatus;
}

@end
